package com.musifind.utils;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.util.Log;

public class SessionManager {
	public static final String TAG = "sessionManager";
	private static final String PREF_NAME = "AndroidPref";
	public static final String REG_ID = "regId";
	public static final String APP_VERSION = "appVersion";
	public static final String USER_ID = "kullaniciId";
	public static final String IS_FACE_USER = "isFaceUser";
	public static final String USERNAME = "userFirstname";
	public static final String USER_PICTURE = "userPicture";
	private SharedPreferences pref;
	private Editor editor;
	private Context context;
	private int PRIVATE_MODE = 0;

	// kontrol edilecek
	// private String sessionId;
	// private static final String IS_LOGIN = "IsLoggedIn";
	// public static final String KEY_NAME = "name";
	// public static final String KEY_EMAIL = "email";
	// public static final String SESSION_ID = "sessionId";

	public SessionManager(Context context) {
		this.context = context;
		pref = context.getSharedPreferences(PREF_NAME, PRIVATE_MODE);
		editor = pref.edit();
	}

	public void createGCMRegIdSession(String regId) {
		editor.putString(REG_ID, regId);
		editor.putInt(APP_VERSION, new AppUtil(context).getAppVersion());
		editor.commit();
	}

	public String getGCMRegistrationId() {
		String registrationId = pref.getString(REG_ID, "");
		if (registrationId.isEmpty()) {
			Log.i(TAG, "Registration not found.");
			return "";
		}
		int registeredVersion = pref.getInt(APP_VERSION, Integer.MIN_VALUE);
		int currentVersion = (new AppUtil(context)).getAppVersion();
		if (registeredVersion != currentVersion) {
			Log.i(TAG, "App version changed.");
			return "";
		}
		return registrationId;
	}

	public void createUserId(int userId) {
		editor.putInt(USER_ID, userId);
		editor.commit();
	}

	public int getUserId() {
		int userId = pref.getInt(USER_ID, 0);
		return userId;
	}

	public void setFaceUser(Boolean faceUser) {
		editor.putBoolean(IS_FACE_USER, faceUser);
		editor.commit();
	}

	public String getUsername() {
		String userName = pref.getString(USERNAME, "");
		return userName;
	}

	public String getUserPicture() {
		String userName = pref.getString(USER_PICTURE, "");
		return userName;
	}

	public Boolean isFaceUser() {
		Boolean faceUser = pref.getBoolean(IS_FACE_USER, false);
		return faceUser;
	}

	public Boolean isGuest() {
		int userId = pref.getInt(USER_ID, 0);
		return userId == 0 ? true : false;
	}

	public final Context getContext() {
		return context;
	}

	public void setProperty(String key, Object value) {
		if (value instanceof String)
			editor.putString(key, (String) value);
		else if (value instanceof Boolean)
			editor.putBoolean(key, (Boolean) value);
		else if (value instanceof Float)
			editor.putFloat(key, (Float) value);
		else if (value instanceof Long)
			editor.putLong(key, (Long) value);
		editor.commit();
	}

	// kontrol et
	// public void createLoginSession(String name, String email, String sessionId) {
	// editor.putBoolean(IS_LOGIN, true);
	// editor.putString(KEY_NAME, name);
	// editor.putString(KEY_EMAIL, email);
	// editor.putString(SESSION_ID, sessionId);
	// editor.commit();
	// }

	// public void checkLogin(String className) {
	// if (!this.isLoggedIn()) {
	// Class cls = null;
	// try {
	// cls = Class.forName(className);
	// } catch (ClassNotFoundException e) {
	// e.printStackTrace();
	// }
	// Intent i = new Intent(context, cls);
	// i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
	// i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
	// context.startActivity(i);
	// }
	// }

	// public HashMap<String, String> getUserDetails() {
	// HashMap<String, String> user = new HashMap<String, String>();
	// user.put(KEY_NAME, pref.getString(KEY_NAME, null));
	// user.put(KEY_EMAIL, pref.getString(KEY_EMAIL, null));
	// return user;
	// }

	public void logoutUser(String className) {
		editor.clear();
		editor.commit();

		Class cls = null;
		try {
			cls = Class.forName(className);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		Intent i = new Intent(context, cls);
		i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		context.startActivity(i);
	}

	// -----------------------------------------
	// public boolean isLoggedIn() {
	// return pref.getBoolean(IS_LOGIN, false);
	// }
	//
	// public String getSessionId() {
	// return pref.getString(SESSION_ID, null);
	// }

}
